﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace PlantsVsZombies
{
    public enum MyEnum
    {
        Up,
        Down,
        LeftTop
    }

    public abstract class NormalPlant : Plant
    {
        protected MyEnum Myenum = MyEnum.Up;

        // vi tri de dua hoa ve
        private Sprite _sun;

        private float _timeForSun; // khoang cach giua 2 lan cho Sun.
        private Vector2 _target = Vector2.Zero;
        private float _time;

        public float TimeForSun
        {
            get { return _timeForSun; }
            set { _timeForSun = value; }
        }

        public Sprite Sun
        {
            get { return _sun; }
            set { _sun = value; }
        }


        public override void Update(GameTime gameTime)
        {
            // thoi diem no hoa
            if (Math.Abs(_time - _timeForSun) < 0.05 && !_sun.Visible)
            {
                _sun.Visible = true;
                Vector2 temp;
                temp.X = Position.X - Size.X*0.2f;
                temp.Y = Position.Y - Size.Y + 10;
                _sun.Position = temp;
                _time = 0;
                Myenum = MyEnum.Up;
            }

            //neu no hoa roi
            if (_sun.Visible)
            {
                //cho hoa bay len
                Vector2 pos = _sun.Position;
                if (pos.Y > Position.Y - 100 && Myenum == MyEnum.Up)
                {
                    pos.Y--;
                }

                if (Math.Abs(pos.Y - (Position.Y - 100)) < 1.0f && Myenum == MyEnum.Up)
                    Myenum = MyEnum.Down;

                //cho hoa bay xuong
                if (pos.Y < (Position.Y - Size.Y) && Myenum == MyEnum.Down)
                    pos.Y++;
                _sun.Position = pos;

                //click chon hoa
                MouseState ms = Mouse.GetState();
                if (ms.LeftButton == ButtonState.Pressed && _sun.IsClicked(new Vector2(ms.X, ms.Y)))
                {
                    Myenum = MyEnum.LeftTop;
                }

                // di chuyen hoa ve dau man hinh
                if (Myenum == MyEnum.LeftTop)
                {
                    Vector2 temp = _sun.Position;
                    if (temp.X > _target.X)
                        temp.X -= 3;
                    temp.Y = LinearEquations(temp.X, _sun.Position, _target);
                    _sun.Position = temp;
                }
                if (_sun.Position.X <= _target.X && _sun.Position.Y <= _target.Y)
                {
                    _time = 0;
                    _sun.Visible = false;
                }
            }
            _time += 0.1f;
            base.Update(gameTime);
        }

        // tim phuong trinh duong thang dua hoa ve dich
        public float LinearEquations(float x, Vector2 source, Vector2 target)
        {
            float a = (source.Y - target.Y)/(source.X - target.X);
            float b = source.Y - a*source.X;
            return a*x + b;
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            if (_sun.Visible)
                _sun.Draw(spriteBatch);

            spriteBatch.DrawString(Level.font, _sun.Position.ToString(), new Vector2(200, 100), Color.White);
            base.Draw(spriteBatch);
        }
    }
}